﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace CRMSimple.LogServices.MergeTool
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            listBox1.DisplayMember = "DisplayName";
        }
        private void btn_OpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog d = new OpenFileDialog();
            d.Filter = "(*.txt)|*.txt";
            d.Multiselect = true;
            if (d.ShowDialog() == DialogResult.OK)
            { 
                //选择文件
                tb_OpenFilePath.Text = Regex.Replace(Path.GetDirectoryName(d.FileName), "\\[0-9]+\\", "");
                var paths = d.FileNames;

                loadFile(paths);

            }
        }
      
        private void loadFile(params string[] paths)
        {
            if (paths == null || paths.Length==0) { MessageBox.Show("文件路径无效!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; }
            foreach (var path in paths)
            {
                if(File.Exists(path))
                {
                    var item = new ListBoxItem() { DisplayName = Path.GetFileName(path), FullFileName = path };
                    listBox1.Items.Add(item);
                }
            }
           
        }
        private StreamReader CreateStreamReader(string path)
        { 
            return new StreamReader(path);
        }
        private string GetAppKey(string fileName)
        {
            //string fileRule = "{0}_{1}.{2}";
            //var fileName=Path.GetFileNameWithoutExtension(path);
            var file=fileName.Split('_');
            return file != null && file.Length>0?file[0]:string.Empty;

        }

        IDictionary<string, IList<string>> dic = new Dictionary<string, IList<string>>();
        private void btn_Merge_Click(object sender, EventArgs e)
        {
            foreach (ListBoxItem item in listBox1.Items)
            {
                using (StreamReader stream = CreateStreamReader(item.FullFileName))
                {
                    //dic.Add(GetAppKey(item.DisplayName), stream.ReadToEnd());
                }
            }

           
        }

        internal class ListBoxItem
        {
            public string DisplayName
            {
                get;
                set;
            }

            public string FullFileName
            {
                get;
                set;
            }

            public override string ToString()
            {
                return DisplayName;
            }
        }

        private void btn_OutPutPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog d = new FolderBrowserDialog();
            if (d.ShowDialog() == DialogResult.OK)
            {
                //选择文件
                tb_OutPutPath.Text = d.SelectedPath;
            }
        }
    }
}
